# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        潘炎珍  
# Name:         demo07
# Description:
# Author:       98745
# Date:         2019/3/16
#-------------------------------------------------------------------------------

from twisted.internet import protocol,reactor
host='localhost'
port=9876
class Myprotocol(protocol.Protocol):
    def senddata(self):
        data=input('>')
        if data:
            print('......正在发送 %s' % data)
            self.transport.write(data.encode(encoding='utf-8'))
        else:
            self.transport.loseConnection()
    def connectionMade(self):
        self.senddata()
    def dataReceived(self, data):
        print(data.decode('utf-8'))
        self.senddata()
class MyFactory(protocol.ClientFactory):
    protocol=Myprotocol
    clientConnectionLost=clientConnectionLostFailed=lambda self,connector,reason:reactor.stop()
reactor.connectTCP(host,port,MyFactory())
reactor.run()
